<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ForetellController
 *
 * @author Sorasak Siangsanan
 */
class ForetellController extends Controller {

    public function actionIndex() {
        $model = new ForetellForm();
        $model->type = 1;

        if (isset($_POST['ForetellForm'])) {
            $model->birthday = $_POST['ForetellForm']['birthday'];
            $model->attributes = $_POST['ForetellForm'];
            $model->time_of_birthday = $_POST['ForetellForm']['time_of_birthday'];
            $model->package = $_POST['ForetellForm']['package'];
            $model->foretell_type_1 = $_POST['ForetellForm']['foretell_type_1'];
            $model->foretell_type_2 = $_POST['ForetellForm']['foretell_type_2'];
            $model->foretell_type_3 = $_POST['ForetellForm']['foretell_type_3'];
            $model->foretell_type_4 = $_POST['ForetellForm']['foretell_type_4'];
            $model->foretell_type_5 = $_POST['ForetellForm']['foretell_type_5'];
            $model->remark = $_POST['ForetellForm']['remark'];
            $model->price_minimum = $_POST['ForetellForm']['price_minimum'];
            if ($model->validate() && $model->foretell()) {
                $message = Yii::t('wait', 'foretellSaved');
                $redirectPage = Yii::app()->getBaseUrl(true) . '/index.php?r=payment';
                $this->redirect(array('/wait', 'message' => $message, 'redirectPage' => $redirectPage));
                return;
                //$this->redirect(Yii::app()->user->returnUrl);
            }
        }

        $Criteria = new CDbCriteria();
        $Criteria->condition = "active = true";
        $package = Package::model()->findAll($Criteria);
        if (empty($model->package)) {
            $price = Package::model()->findByPk($package[0]['package_id'])->price;
        } else {
            $price = Package::model()->findByPk($model->package)->price;
        }

        //get customer from user id ::::: user cust detail for default telephone
        $cust = Customer::model()->findByPk(Yii::app()->user->id);
        
        $this->render('index', array('model' => $model, 'package' => $package, 'price' => $price, 'cust'=>$cust));
    }

    public function actionForetellPrice() {
        $package_id = $_POST['package_id'];
        $data = Package::model()->findByPk($package_id);
        echo $data->price.'|'.$data->package_desc;
    }

    public function actionUpdate() {

        $id = $_GET["id"];
        if (isset($id)) {
            $order = Order::model()->findByPk($id);
            if (!isset($order) || ($order->status_id != 1 && $order->status_id != 2)) {
                $this->redirect(array('foretell/index'));
            }
        } else {
            $this->redirect(array('foretell/index'));
        }

        $model = new ForetellUpdateForm();

        if (isset($_POST['ForetellUpdateForm'])) {
            $model->birthday = $_POST['ForetellUpdateForm']['birthday'];
            $model->attributes = $_POST['ForetellUpdateForm'];
            $model->time_of_birthday = $_POST['ForetellUpdateForm']['time_of_birthday'];
            $model->profile_image_name = $_POST['ForetellUpdateForm']['profile_image_name'];
            $model->foretell_type_1 = $_POST['ForetellUpdateForm']['foretell_type_1'];
            $model->foretell_type_2 = $_POST['ForetellUpdateForm']['foretell_type_2'];
            $model->foretell_type_3 = $_POST['ForetellUpdateForm']['foretell_type_3'];
            $model->foretell_type_4 = $_POST['ForetellUpdateForm']['foretell_type_4'];
            $model->foretell_type_5 = $_POST['ForetellUpdateForm']['foretell_type_5'];
            $model->remark = $_POST['ForetellUpdateForm']['remark'];
            $model->price_minimum = $_POST['ForetellUpdateForm']['price_minimum'];
            if ($model->validate() && $model->foretell($order)) {
                $message = Yii::t('wait', 'foretellSaved');
                $redirectPage = Yii::app()->getBaseUrl(true) . '/index.php?r=foretell/edit';
                $this->redirect(array('/wait', 'message' => $message, 'redirectPage' => $redirectPage));
                return;
            }
        }

        $Criteria = new CDbCriteria();
        $Criteria->condition = "active = true";
        $package = Package::model()->findAll($Criteria);
        $package_select = Package::model()->findByPk($order->package_id);

        $this->render('update', array('model' => $model, 'order' => $order, 'package' => $package, 'package_select' => $package_select));
    }

    public function actionView() {

        $id = $_GET["id"];
        if (isset($id)) {
            $order = Order::model()->findByPk($id);
            if (!isset($order)) {
                $this->redirect(array('foretell/index'));
            }
        } else {
            $this->redirect(array('foretell/index'));
        }

        $this->render('view', array('order' => $order));
    }

    public function actionDelete() {

        $id = $_GET["id"];
        if (empty($id)) {
            $this->redirect(array('foretell/index'));
        }

        $order = Order::model()->findByPk($id);
        if ($order->status_id == 1) {
            $order->status_id = 6;
        } else if ($order->status_id == 2) {
            $order->status_id = 7;
        } else {
            $this->redirect(array('foretell/index'));
        }
        $order->update_date = date('Y-m-d H:i:s');
        if ($order->update()) {
            $this->redirect(array('foretell/index'));
        }
//        if (Order::model()->deleteByPk($id)) {
//            if (!empty($order->picture) && strpos($order->picture, ImageUtil::PREFIX_FRIEND) > 0) {
//                @unlink(Yii::app()->basePath . '/../' . $order->picture);
//            }
//            $this->redirect(array('foretell/index'));
//        }
    }

    public function actionEdit() {
        $model = new Order('searchUser');
        $model->unsetAttributes();  // clear any default values
        if (isset($_GET['Order'])) {
            $model->attributes = $_GET['Order'];
        }
        $this->render('edit', array('model' => $model));
    }

    public function actionFinish() {
        $model = new Order('searchUser');
        $model->unsetAttributes();  // clear any default values
        if (isset($_GET['Order'])) {
            $model->attributes = $_GET['Order'];
        }
        $this->render('finish', array('model' => $model));
    }

}
